<?php
class IncomesController extends AppController {

	var $name = 'Incomes';
	var $uses= array('Member','Invoice');
	
	function index() {
		$this->Member->recursive = 0;
		$conditions=array('Member.type'=>0);
		$this->paginate = array('conditions'=>$conditions,'order'=>'Member.id DESC','limit' =>30);
        $member = $this->paginate('Member');
		$this->set('members', $member);
		
		$this->set('namesOfLevel', $this->Tree->namesOfLevel);
		$info = $this->Session->read('info');
		$this->set('nodes', $info);
	}
   function view($id=null) {
       $this->set('namesOfLevel', $this->Tree->namesOfLevel);
	   if(empty($id)){
			$name=$_SERVER['HTTP_REFERER'];
			$this->redirect($name); 
		}
		else{
		    if (!empty($this->data['month'])){
                $history = $this->History->findByMonth($this->data['month']);
                $data = json_decode($history['History']['data'], true);
                $this->set('member', $data[$id]);
				
				$this->Tree->nodes = $data;
				$this->set('viewTree', $this->Tree->printTree($this->Tree->nodes[$id]));

            } else {
                $info = $this->Session->read('info');
                $this->set('member', $info[$id]);
				
                $this->Tree->nodes = $info;
				$this->set('viewTree', $this->Tree->printTree($this->Tree->nodes[$id]));

            }
            if (is_array($this->data)){
                $this->data = array_merge($this->data, $this->Member->read(null,$id));
            } else {
                $this->data = $this->Member->read(null,$id);
            }
		}

	}
  //seach tai khoan view thong tin 
	function search() { 		
		if(isset($_POST['keyword'])){
				$keyword = $_POST['keyword'];
				$this->Session->write('keyword',$keyword);
		}else{
			$keyword=$this->Session->read('keyword');
		}
	
		 $conditions=array('OR'=>array('Member.name LIKE'=>'%'.$keyword.'%','Member.user LIKE'=>'%'.$keyword.'%'));
		 $this->paginate = array('conditions'=>$conditions,'order'=>'Member.id DESC','limit'=>30);
		 $members = $this->paginate('Member');
		 $this->set('members',$members);

	}	

    
    /**
     * 
     * Enter description here ...
     */
    function saveHistory(){
       $this->Tree->calculateCompany();
       $this->Tree->updateMemberInfo();
       
       $data = json_encode($this->Tree->nodes);
       
       $history = array();
       $history['History']['month'] = date("m-Y");
       $history['History']['data'] = $data;
       
       $chkHistory = $this->History->findByMonth($history['History']['month']);
       
       if (!$chkHistory){
           $this->History->save($history);
       } else {
           $chkHistory['History']['month'] = date("m-Y");
           $chkHistory['History']['data'] = $data;
           $this->History->save($chkHistory);
       }
	   // lay nhung hoa don co fag=0
	   $update=$this->Invoice->find('all',array('conditions'=>array('Invoice.fag'=>0)));
	   foreach($update as $up){
		   $arr[]=array('Invoice'=>array(
		   									'id'=>$up['Invoice']['id'],
											'fag'=>1
											));
		
	  }
	  $this->Invoice->saveall($arr);
	   
       echo "<script>alert('".json_encode('Đã thực hiện chốt doanh số!')."');</script>";
       echo "<script>location.href='".DOMAIN."/incomes'</script>";
    }
    
    /**
     * 
     * Enter description here ...
     */
    function config(){
        $this->set('namesOfLevel', $this->Tree->namesOfLevel);
        if (!isset($this->data)){
            $this->data = array(
                'DirectCommission' => $this->Tree->directCommission,
                'IndirectCommission' => $this->Tree->indirectCommission,
                'LeadCommission' => $this->Tree->leadCommission,
                'GlobalCommission' => $this->Tree->globalCommission,
            );
        } else {
                $history = array();
                $history['History']['month'] = "00-0000";
                $history['History']['data'] = json_encode($this->data);
                
                $chkHistory = $this->History->findByMonth("00-0000");
                 
                if (!$chkHistory){
                    $this->History->save($history);
                } else {
                    $chkHistory['History']['data'] = $history['History']['data'];
                    $this->History->save($chkHistory);
                }
        }
    }
}
?>